package com.pine.app.module.security.core.authorize.metadataSource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * @author xiaoyuan
 * @create 2020/3/23 16:31
 **/
public class YmlSecurityLoadMethodDataSource implements SecurityLoadMethodDataSource {

    private AuthorizeProperties authorizeProperties;



    public YmlSecurityLoadMethodDataSource(final AuthorizeProperties authorizeProperties) {
        this.authorizeProperties = authorizeProperties;
    }


    @Override
    public MultiValueMap<String, String> getMethodDataSource() {
        MultiValueMap<String, String> filterChainDefinitionMap = new LinkedMultiValueMap();
        if (authorizeProperties == null || authorizeProperties.getConfig() == null) {
            return filterChainDefinitionMap;
        }
        authorizeProperties.getConfig().forEach(s -> {
            if (StringUtils.isNotEmpty(s)) {
                String[] ex = s.split("=");
                if (ex.length == 2 && StringUtils.isNotEmpty(ex[0]) && StringUtils.isNotEmpty(ex[1])) {
                    filterChainDefinitionMap.add(ex[0].trim(), ex[1].trim());
                }
            }
        });
        return filterChainDefinitionMap;
    }
}
